﻿using System;
using System.Collections.Generic;
using System.Text;

/*
 * Author:Conis
 * Date:
 * Description:JSON的基础组件
 */

namespace Coins.Squirrel.Nodous.JSON
{
    /// <summary>
    /// JSON操作的抽象类
    /// </summary>
    public abstract class JSONNodousComponent
    {

        #region 属性及变量
        protected string value;     //JSON节点的名称
        protected string name;      //JSON节点的值
        protected StringBuilder container;  //保存JSON数据容器
        protected int currentIndex; //当前处理的节点索引
        protected int brotherCount;     //当前片的节点的兄弟节点总数
        protected List<JSONNodousComponent> list;

        /// <summary>
        /// 父节点
        /// </summary>
        public JSONNodousComponent Parent { get; set; }
        #endregion

        #region 其它方法
        /// <summary>
        /// 构靠一个节点
        /// </summary>
        /// <param name="name">节点的名称</param>
        /// <param name="value">节点的值</param>
        protected JSONNodousComponent(string name, string value)
        {
            this.name = this.FormatText(name);
            this.value = this.FormatText(value);
            this.list = new List<JSONNodousComponent>();
        }

        /// <summary>
        /// 把字符串格式化为有双引号的字符串
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        protected string FormatText(string text)
        {
            //替换双引号及反斜杠
            text = text.Replace("\\", "\\\\").Replace("\"", "\\\"");
            text = text.Replace((char)13, (char)0);
            text = text.Replace((char)10, (char)0);
            return string.Format("\"{0}\"", text);
        }

        /// <summary>
        /// 创建JSON数据
        /// </summary>
        protected void Create()
        {
            this.CreatePreifx();
            int index = 1;
            foreach (JSONNodousComponent item in list)
            {
                item.container = this.container;
                item.currentIndex = index;
                item.brotherCount = list.Count;
                item.Create();
                index++;
            }
            this.CreateSuffix();
        }
        #endregion

        #region 节点操作
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="c"></param>
        protected void Add(JSONNodousComponent c)
        {
            list.Add(c);
        }

        /// <summary>
        /// 删除节点节
        /// </summary>
        /// <param name="c"></param>
        public void Remove(JSONNodousComponent c)
        {
            list.Remove(c);
        }

        /// <summary>
        /// 删除指定索引的节点
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= this.list.Count)
            {
                return;
            }
            list.RemoveAt(index);
        }
        #endregion

        #region 抽象方法
        protected abstract void CreatePreifx();
        protected abstract void CreateSuffix();
        #endregion
    }
}
